<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"
	import="com.telecom.tree.*,java.util.*,com.telecom.cms.dao.*,com.telecom.cms.dto.*"%>
<%@page import="java.sql.*"%>
<%@page import="com.telecom.cms.dbconnect.*"%>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>收藏</title>
		<STYLE type=text/css></STYLE>
		<script type="text/javascript" src="dtree.js"></script>
		<script type="text/javascript" src="default.js"></script>
		<link rel="stylesheet" type="text/css" href="images/main.css">
		<SCRIPT type="text/javascript">
function Atomoption (text,value){
	this.text=text;
	this.value=value;
}

var atoms=new Array();

function ChangeGroup()
{ 	
	var i,iGroupIndex;
	iGroupIndex=document.all.groups.selectedIndex;
	iAtomCount=0; 
	while (atoms[iGroupIndex][iAtomCount]!=null) 
	 iAtomCount++;
	 //计算选定组下的原子个数 
	document.all.SrcSelect.length=0;//清空选项数 
	var obj=document.all.ObjSelect;
	var objCount=obj.length;
	for(i=0;i<=iAtomCount-1;i++){//改变下拉菜单的内容 
		var flag=0;
		for(j=0;j<=objCount-1;j++){
			if(obj.options[j].value==atoms[iGroupIndex][i].value){
				flag++;
			}
		}
		if(flag==0)
		document.all.SrcSelect.add(new Option(atoms[iGroupIndex][i].text,atoms[iGroupIndex][i].value));
	}
	document.all.SrcSelect.focus();
}

function moveUp()  
{ 
var theObj=document.all.ObjSelect;
for(var i=1;i<theObj.length;i++)
 {
  if( theObj.options[i].selected && !theObj.options[i-1].selected )
  {
   theObj.options[i].swapNode(theObj.options[i-1]);//与上面一个交换
  }
 }
}
  
function moveDown()
{ 
var theObj=document.all.ObjSelect;
for(var i=theObj.length-2;i>-1;i--)
 {
  if( theObj.options[i].selected && !theObj.options[i+1].selected )
  {
   theObj.options[i].swapNode(theObj.options[i+1]);
  }
 }
}
  
  
function moveLeftOrRight(fromObj,toObj)
{
 var lengthOfToObj=toObj.length;
 for(var i=fromObj.length-1;i>-1;i--)
 {
  if(fromObj.options[i].selected)
  {
   toObj.add(new Option(fromObj.options[i].text,fromObj.options[i].value),lengthOfToObj);//new option(text,value,true,true)
   toObj.options[lengthOfToObj].selected=true;
   fromObj.options[i].removeNode(true);
  }
 }
}

function cancel(){
	window.location.href="rstest.jsp";
}

function chacksel(o){
  document.getElementById("operation").value=o;
   if(o=="del"){
   		if(confirm("是否删除收藏的模板？")){
	  		document.form1.submit();
   		}else{
   			return;
   		}
    }
  obj=document.form1.ObjSelect;
        if(obj.length<1){   
          alert("请定制用户分群的结果字段!!");   
        }else{
        for(i=0;i<obj.length;i++){
			obj.options[i].selected=true;
		} 
  			document.form1.submit();
  		} 
}

function querymodel(o){
	if(o.value==0){
		window.location.href="rstemplatedef.jsp";
	}else{
		document.form1.action="atomGet4RSModelDEF.do"
		document.getElementById("operation").value="modify";
		document.form1.submit();
	}
} 

function changeOrderRule(sel){
	var i=sel.selectedIndex;
	var value=sel.options[i].value;
	var text=sel.options[i].innerHTML;
	vstate=value.split("#");
	tstate=text.split("[");
	if(vstate[1]==null){
	sel.options[i].value=vstate[0]+"#asc";
	sel.options[i].innerHTML=tstate[0]+"[asc]";
	}
	if(vstate[1]=="asc"){
	sel.options[i].value=vstate[0]+"#desc";
	sel.options[i].innerHTML=tstate[0]+"[desc]";
	}
	if(vstate[1]=="desc"){
	sel.options[i].value=vstate[0];
	sel.options[i].innerHTML=tstate[0];
	}
}
</script>
	</head>
	<body>
		<fieldset>
			<legend>
				&nbsp;
				<font size="3">结果字段选取</font>
			</legend>
			<form name="form1" method="post" action="rsModelOperation.do">
				<table align="center" border="0">
					<tr>
						<td align="left">
							&nbsp选择:
							<select id="templateId" name="templateId"
								onchange="querymodel(this)">
								<option value="0" selected>
									新建模板
								</option>
								<%
											Connection con = GetConnect.getCsConnection();
											IDA_TEMPLATEDAO templatedao = new IDA_TEMPLATEDAO(con);
											List templist = templatedao.findAll("20", "admin");
											Iterator templateIt = templist.iterator();
											while (templateIt.hasNext()) {
												IDA_TEMPLATEDTO templatedto = (IDA_TEMPLATEDTO) (templateIt
												.next());
												String templatename = templatedto.getTemplateName();
												String templateid = templatedto.getTemplateId().toString();
								%>
								<option value="<%=templateid%>"
									<%=templateid.equals(request
										.getParameter("templateId")) ? " selected"
								: ""%>>
									<%=templatename%>
								</option>
								<%
									}
								%>
							</select>
						</td>
						<td align="right">
							<input type="button" name="add" id="add" value="添加 "
								onclick="chacksel('add')">
							<input type="button" name="mod" id="mod" value="修改"
								onclick="chacksel('mod')">
							<input type="button" name="del" id="del" value="删除"
								onclick="chacksel('del')">
						</td>
					</tr>
					<tr>
						<td align="center" colspan="3">
							<%
								IDA_TEMPLATEDTO templateinfo = (IDA_TEMPLATEDTO) request
										.getAttribute("templateinfo");
							%>
							<table align="center" border="0">
								<tr>
									<td>
										名称:
									</td>
									<td>
										<input type="text" name="TemplateName" size="59"
											value=<%=templateinfo == null ? "" : templateinfo
					.getTemplateName()%>>
									</td>
								</tr>
								<tr>
									<td>
										描述:
									</td>
									<td>
										<textarea name="TemplateDesc" cols="46" rows="3"><%=templateinfo == null ? "" : templateinfo
					.getTemplateDesc()%></textarea>
									</td>
								</tr>
								<tr align="center">
									<td>
										<input type="hidden" name="TemplateTypeId" value="1">
										<input type="hidden" name="UserId" value="admin">
										<input type="hidden" name="LatnId" value="1000">
										<input type="hidden" id="operation" name="method" value="">
									</td>
								</tr>
							</table>
						</td>
					</tr>
				</table>
				<table border="0" width="600" border="0" align="center">
					<tr>
						<td colspan="4" width="100%">
							请选择结果字段类别:
							<select name="groups" id="groups" onchange="ChangeGroup()">
								<%
									SEG_BIZ_GROUP_DEFDAO dao = new SEG_BIZ_GROUP_DEFDAO(con);
									List grouplist = dao.findAll();
									int j = 0;
									for (int i = 0; i < grouplist.size(); i++) {
										SEG_BIZ_GROUP_DEFDTO groupdto = (SEG_BIZ_GROUP_DEFDTO) grouplist
												.get(i);
										if (groupdto.getATOM_GROUP_ID() == 400006) {
											continue;
										}
								%>
								<option value=<%=groupdto.getATOM_GROUP_ID()%>><%=groupdto.getATOM_GROUP_NAME()%></option>
								<script type="text/javascript">atoms[<%=j%>]=new Array(); </script>
								<%
												SEG_ATOM_GROUP_RELDAO reldao = new SEG_ATOM_GROUP_RELDAO(con);
												List rellist = reldao.findById(groupdto.getATOM_GROUP_ID());
												List atomlist = new ArrayList();
												for (int k = 0; k < rellist.size(); k++) {
													SEG_ATOM_GROUP_RELDTO seg_ATOM_GROUP_RELDTO = (SEG_ATOM_GROUP_RELDTO) rellist
													.get(k);
													SEG_BIZ_ATOM_DEFDAO atomdao = new SEG_BIZ_ATOM_DEFDAO(
													con);
													SEG_BIZ_ATOM_DEFDTO atomdto = atomdao
													.findById(seg_ATOM_GROUP_RELDTO.getBIZ_ATOM_ID());
													if (atomdto.getVALUE_TYPE_ID() != 1000
													&& atomdto.getVALUE_TYPE_ID() != 2000) {
												atomlist.add(atomdto);
													}
												}
												if (atomlist.size() > 0)
													for (int m = 0; m < atomlist.size(); m++) {
												SEG_BIZ_ATOM_DEFDTO showdto = (SEG_BIZ_ATOM_DEFDTO) atomlist
														.get(m);
								%>
								<script type="text/javascript">atoms[<%=j%>][<%=m%>]=new Atomoption('<%=showdto.getBIZ_ATOM_NAME()%>',<%=showdto.getBIZ_ATOM_ID()%>) </script>
								<%
									}
										j++;
									}
								%>
							</select>
						</td>
					</tr>
					<tr>
						<td width="35%">
							可选结果字段:
						</td>
						<td width="15%">

						</td>
						<td width="35%">
							已选结果字段:
						</td>
						<td width="15%">

						</td>
					</tr>
					<tr>
						<td width="35%">
							<select name="SrcSelect" size=20
								style="font-size: 10pt; width: 250px;" multiple>
							</select>
						</td>
						<td width="15%" align="center">
							<a
								href="javascript:moveLeftOrRight(document.all.SrcSelect,document.all.ObjSelect)"><img
									src="images/toright.jpg" border="0"> </a>
							<br>
							<br>
							<a
								href="javascript:moveLeftOrRight(document.all.ObjSelect,document.all.SrcSelect)"><img
									src="images/toleft.jpg" border="0"> </a>
						</td>
						<td width="35%">
							<select name="ObjSelect" size=20
								style="font-size: 10pt; width: 250px;" multiple
								ondblclick="changeOrderRule(this)">
								<%
									List output = (List) request.getAttribute("modelDisplay");
									if (null != output) {
										Iterator it = output.iterator();
										while (it.hasNext()) {
											IDA_TEMPLATE_ATOM_RESULTDTO dto = (IDA_TEMPLATE_ATOM_RESULTDTO) it
													.next();
											String tOrderRule = "";
											String vOrderRule = dto.getResultOrderRule();
											if (!(null == vOrderRule || "".equals(vOrderRule))) {
												tOrderRule = "[" + dto.getResultOrderRule() + "]";
												vOrderRule = "#" + vOrderRule;
											} else {
												vOrderRule = "";
											}
								%>
								<option value="<%=dto.getBizAtomId() + vOrderRule%>"><%=dto.getBizAtomName() + tOrderRule%></option>
								<%
									}
									}
								%>
							</select>
						</td>

						<td width="15%" align="center">
							<a href="javascript:moveUp()"><img src="images/up.jpg"
									border="0"> </a>
							<br>
							<br>
							<a href="javascript:moveDown()"><img src="images/down.jpg"
									border="0"> </a>
						</td>
					</tr>
				</table>
			</form>
			<script type="text/javascript">
					document.all.groups.selectedIndex=0;
					ChangeGroup();
				</script>
		</fieldset>
	</body>
</html>